﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class AquacultureFeedbackListControl : YuanlinUserControl,
        IReplyConverter<AquacultureFeedbackReply, GetListOperation>
    {
        protected Literal ltlTile;
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList aquacultureFeedbacks;
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            AquacultureFeedbackListRequest request = new AquacultureFeedbackListRequest();
            request.CategoryType = categoryType;
            request.PageIndex = pager.PageIndex;
            request.PageSize = pager.PageSize;
            requestBinders.Add(RequestHelper.BuildGetAquacultureFeedbackListRequest(this, request));
        }
        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "AjaxMethod");
        }

        private void BindData()
        {
            SetLtlTitle();
            if (list == null)
            {
                return;
            }

            list.DataSource = aquacultureFeedbacks;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(aquacultureFeedbacks.TotalCount);
                currentPage.TotalRecords = (aquacultureFeedbacks.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }
        private void SetLtlTitle()
        {
            string str = "";
            switch (categoryType)
            {
                case (int)CategoryType.Fishery:
                    {
                        str = ResourceManager.GetString("System_Applications_Yuanlin_FisheryFeedback", "ControlPanelResources.xml");
                    }
                    break;
                case (int)CategoryType.Livestock:
                    {
                        str = ResourceManager.GetString("System_Applications_Yuanlin_LivestockFeedback", "ControlPanelResources.xml");
                    }
                    break;
            }
            ltlTile.Text = str;
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteAquacultureFeedback(int entityId)
        {
            //管理员认为公司添加的实体信息不合法，可以通过这里来把公司添加的实体信息给删除，
            //即删除反馈表数据的同时也会删除公司实体表中对应的数据（目的：对公司乱输入的情况做一个后续的控制）
            BusinessManager.DeleteAquacultureFeedback(entityId);
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int DeleteAquacultureFeedbacks(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    DeleteAquacultureFeedback(entityId);                
                }
            }
            return 0;
        }

        #endregion

        #region IReplyConverter<AquacultureFeedbackReply,GetListOperation> Members

        public void ConvertReply(AquacultureFeedbackReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                aquacultureFeedbacks = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}